package cn.melon.mod.sc;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 存储学生选课信息类, 其中也包括一些对选课信息的操作
 * @author corvo
 */
public class StInfo {
    private String studentNumber;                               // 学号
    private List<String> classTable = new ArrayList<String>();  // 已选课程id的表
    
    /**
     * 设置课程id表, 数据库保存有已选课程的json数组
     * @param jsonArr void
     */
    public void setClassTable(String jsonArr) {         
        JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(jsonArr);
            for (int i = 0; i < jsonArray.length(); i++) {
                this.addClass(jsonArray.getString(i));
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
    }

    public static StInfo toInfo(JSONObject jInfo) throws JSONException {

        StInfo stInfo = new StInfo();
        stInfo.setStudentNumber(jInfo.getString("studentNumber"));
        stInfo.setClassTable(jInfo.getString("classTable"));

        return stInfo;
    }

    public JSONObject toJSON() throws JSONException {
        JSONObject jInfo = new JSONObject();
        jInfo.put("studentNumber", getStudentNumber());
        jInfo.put("classTable", getClassTable());
     
        return jInfo;
    }
    /**
     * 已选课程存为json数组
     * @return String
     */
    public String tableToJson() {
        JSONArray jsonArray = new JSONArray();
        for(String string: classTable) {
            jsonArray.put(string);
        }
        return jsonArray.toString();
    }

    /**
     * 整个对象存为json字符串
     * @Title: toJsonStr
     * @return String
     */
    public String toJsonStr() {
        JSONObject jObj = new JSONObject(); 

        JSONArray jsonArray = new JSONArray();

        for (String string : classTable) {
            jsonArray.put(string);
        }
        
        try {
            jObj.put("student_number", studentNumber);
            jObj.put("class_table", jsonArray);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jObj.toString();
    }
    
    /**
     * 使用json数组更新数据
     * @param table 传入的json字符串
     */
    public void updateStr(String table) {
        try {
            JSONArray jtable = new JSONArray(table);
            classTable = new ArrayList<>();
            for (int i = 0; i < jtable.length(); i++) {
                classTable.add(jtable.getString(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }

    public List<String> getClassTable() {
        return classTable;
    }

    public void setClassTable(List<String> classTable) {
        this.classTable = classTable;
    }

    public void addClass(String classNumber) {
        if (classTable.contains(classNumber)) {
            return ;
        }
        classTable.add(classNumber);
    }

    public void remove(String classNumber) {
        classTable.remove(classNumber);
    }

    public int length() {
        return classTable.size();
    }
}
